﻿using BLL.Dal;
using BLL.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SysAdmin_Menu_List : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["paramType"] != null && Request["paramType"] != "")
        {
            switch (Request["paramType"])
            {
                case "QueryTreeMenu": GetTreeMenu(); break;
                case "AddMenu": AddMenu(); break;
                case "DelMenu": DelMenu(); break;
                case "QueryModel": QueryModel(); break;
                case "UpdateModel": UpdateModel(); break;
                default: break;
            }
        }
    }

    /// <summary>
    /// 获取所有的树形Org数据
    /// </summary>
    void GetTreeMenu()
    {

        SysMenuDAL menuBus = new SysMenuDAL();
        DataTable dtMenu = menuBus.GetList("").Tables[0];

        System.Text.StringBuilder sbMenu = new System.Text.StringBuilder();
        sbMenu.Append("[");
        System.Text.StringBuilder sbMenuRoot = new System.Text.StringBuilder();
        DataRow[] rowsRoot = dtMenu.Select("parentcode='Root'");
        //第一级节点
        for (int i = 0; i < rowsRoot.Length; i++)
        {
            sbMenuRoot.Append("{");
            sbMenuRoot.Append("\"key\":" + "\"" + rowsRoot[i]["menuCode"] + "\",");
            sbMenuRoot.Append("\"value\":" + "\"" + rowsRoot[i]["menuName"] + "\",");
            sbMenuRoot.Append("\"menuCode\":" + "\"" + rowsRoot[i]["menuCode"] + "\",");
            sbMenuRoot.Append("\"iconCls\":" + "\"" + rowsRoot[i]["iconName"] + "\",");
            sbMenuRoot.Append("\"indexNum\":" + "\"" + rowsRoot[i]["indexNum"] + "\",");
            sbMenuRoot.Append("\"isLeaf\":" + "\"" + rowsRoot[i]["isLeaf"] + "\",");
            sbMenuRoot.Append("\"url\":" + "\"" + rowsRoot[i]["url"] + "\",");


            System.Text.StringBuilder sbMenuChild = new System.Text.StringBuilder();
            DataRow[] rowsChild = dtMenu.Select("parentcode='" + rowsRoot[i]["menuCode"] + "'");
            //第二级节点
            for (int j = 0; j < rowsChild.Length; j++)
            {
                sbMenuChild.Append("{");
                sbMenuChild.Append("\"key\":" + "\"" + rowsChild[j]["menuCode"] + "\",");
                sbMenuChild.Append("\"value\":" + "\"" + rowsChild[j]["menuName"] + "\",");
                sbMenuChild.Append("\"menuCode\":" + "\"" + rowsChild[j]["menuCode"] + "\",");
                sbMenuChild.Append("\"iconCls\":" + "\"" + rowsChild[j]["iconName"] + "\",");
                sbMenuChild.Append("\"indexNum\":" + "\"" + rowsChild[j]["indexNum"] + "\",");
                sbMenuChild.Append("\"isLeaf\":" + "\"" + rowsChild[j]["isLeaf"] + "\",");
                sbMenuChild.Append("\"url\":" + "\"" + rowsChild[j]["url"] + "\"");
                sbMenuChild.Append("}");
                sbMenuChild.Append(",");
            }

            sbMenuRoot.Append("\"children\":" + "[" + sbMenuChild.ToString().TrimEnd(',') + "]");
            sbMenuRoot.Append("}");
            sbMenuRoot.Append(",");
        }

        sbMenu.Append(sbMenuRoot.ToString().TrimEnd(','));
        sbMenu.Append("]");
        WebHelper.HttpResponseJson(sbMenu.ToString());
    }

    class TreeGridItem
    {
        /// <summary>
        /// 上级节点标识
        /// </summary>
        public string pid { get; set; }
        /// <summary>
        /// 上级节点名称
        /// </summary>
        public string pname { get; set; }
        public string attributes { get; set; }
        /// <summary>
        /// 本级节点标识
        /// </summary>
        public string id { get; set; }
        /// <summary>
        /// 本级节点名称
        /// </summary>
        public string text { get; set; }
        /// <summary>
        /// 本级节点url
        /// </summary>
        public string url { get; set; }
        /// <summary>
        /// 图标标识
        /// </summary>
        public string iconCls { get; set; }
        /// <summary>
        /// 菜单级别
        /// </summary>
        public string seq { get; set; }
    }

    /// <summary>
    /// 增加菜单操作
    /// </summary>
    void AddMenu()
    {
        SysMenuDAL sysMenuDal = new SysMenuDAL();
        SysMenu sysMenu = new SysMenu();
        sysMenu.menuCode = ReqHelper.Get<string>("menu_key");
        sysMenu.menuName = ReqHelper.Get<string>("menu_name");
        //sysMenu.parentCode = ReqHelper.Get<string>("");
        sysMenu.parentCode = ReqHelper.Get<string>("parent_code");
        sysMenu.indexNum = 0;
        sysMenu.isLeaf = true;
        sysMenu.isActive = false;
        if (!string.IsNullOrEmpty(Request["is_active"]) && Request["is_active"] == "on")
        {
            sysMenu.isActive = true;
        }
        sysMenu.url = ReqHelper.Get<string>("menu_url");
        sysMenu.menuLevel = 0;
        sysMenu.iconName = ReqHelper.Get<string>("iconCls");

        object obj = new { success = true, msg = "数据操作成功" };
        try
        {
            obj = new { success = sysMenuDal.Add(sysMenu), msg = "数据操作成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }


    /// <summary>
    /// 删除菜单
    /// </summary>
    void DelMenu()
    {
        string strCode = ReqHelper.Get<string>("menuCode");
        SysMenuDAL sysMenuDal = new SysMenuDAL();
        object obj = new { success = sysMenuDal.Delete(strCode) };
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }

    /// <summary>
    /// 查询菜单
    /// </summary>
    void QueryModel()
    {
        string strCode = ReqHelper.Get<string>("menuCode");
        SysMenuDAL sysMenuDal = new SysMenuDAL();
        SysMenu sysMenu = sysMenuDal.GetModel(strCode);

        object obj = new { success = true, message = sysMenu };
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }

    /// <summary>
    /// 更新菜单
    /// </summary>
    void UpdateModel()
    {

        SysMenuDAL sysMenuDal = new SysMenuDAL();
        SysMenu sysMenu = sysMenuDal.GetModel(ReqHelper.Get<string>("menu_key"));
        sysMenu.menuName = ReqHelper.Get<string>("menu_name");
        sysMenu.isActive = false;
        if (!string.IsNullOrEmpty(Request["is_active"]) && Request["is_active"] == "on")
        {
            sysMenu.isActive = true;
        }
        sysMenu.url = ReqHelper.Get<string>("menu_url");
        sysMenu.iconName = ReqHelper.Get<string>("iconCls");

        object obj = new { success = true, msg = "数据操作成功" };
        try
        {
            obj = new { success = sysMenuDal.Update(sysMenu), msg = "数据操作成功" };
        }
        catch (Exception error)
        {
            obj = new { success = false, msg = error.Message };
        }
        string response = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        WebHelper.HttpResponseJson(response);
    }
}